

package com.zf.yichat.controller.fanli;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.response.FanliIndexDto;
import com.zf.yichat.mapper.FanliApiMapper;
import com.zf.yichat.service.FanliService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("fanli")
public class FanliController extends BaseController {
    @Resource
    FanliApiMapper fanliApiMapper;
    @Resource
    FanliService fanliService;

    @RequestMapping("receive/index")
    public String receiveIndex() {
        return "fanli/receive-index";
    }

    @RequestMapping("receive/list")
    @ResponseBody
    public FsResponse receiveList(Integer page, Integer limit, Integer checkStatus) {
        PageHelper.startPage(page, limit);
        List<FanliIndexDto> list = fanliApiMapper.selectReceiveList(checkStatus);
        return DtoChangeUtils.getPageList(list, v -> v);
    }

    @RequestMapping("apply/index")
    public String index() {
        return "fanli/check-index";
    }

    @RequestMapping("apply/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Integer checkStatus, Integer checkType) {


        PageHelper.startPage(page, limit);
        List<FanliIndexDto> list = fanliApiMapper.selectList(checkStatus, checkType);
        return DtoChangeUtils.getPageList(list, v -> v);
    }


    @RequestMapping("apply/check")
    @ResponseBody
    public FsResponse check(Long id, Integer status, String reason) {
        //通过
        if (status == 1) {
            return fanliService.checkSusccess(id);
        } else if (status == 2) {
            //拒绝
            return fanliService.checkFail(id, reason);
        }
        return FsResponseGen.fail();
    }

    @RequestMapping("/apply/detail/index")
    public String detail(Long id) {
        FanliIndexDto dto = fanliApiMapper.selectById(id);
        dto.setImgsList(dto.getImgs() == null ? null : JSONArray.parseArray(dto.getImgs(), String.class));
        dto.setSmallImagesList(dto.getSmallImages() == null ? null : JSONArray.parseArray(dto.getSmallImages(), String.class));
        getRequest().setAttribute("fanli", dto);
        return "fanli/check-detail";
    }

}
